/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import bean.BarangGudang;
import bean.Pemesanan;
import bean.Pengiriman;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author TI USD
 */
@WebServlet(urlPatterns = {"/KirimBarang"})
public class KirimBarang extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Pengiriman pengiriman = new Pengiriman();
        String kode_pemesanan = request.getParameter("kodepemesanan");
        String harga_barang = request.getParameter("hargabarang");
        String jumlah_barang = request.getParameter("jumlahbarang");
        if ("".equals(jumlah_barang) || "".equals(harga_barang) || !harga_barang.matches("[\\d]*") || !jumlah_barang.matches("[\\d]*")) {
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("form_kirim_barang.jsp");
            requestDispatcher.forward(request, response);
        } else {
            Pemesanan pemesanan = new Pemesanan();
            BarangGudang barangGudang = new BarangGudang();
            barangGudang.setIdBarang(pemesanan.tampilIdBarang(kode_pemesanan));
            barangGudang.setJumlahBarang(Double.parseDouble(jumlah_barang));
            pemesanan.setKodeUpdate(kode_pemesanan);
            pemesanan.setJumlahBarang(Double.parseDouble(jumlah_barang));
            if (pemesanan.bandingJumlahBarangPemesanan() == true && barangGudang.bandingJumlahBarang()) {
                if (Double.parseDouble(jumlah_barang) < pemesanan.tampilJumlahBarangPemesanan()) {
                    pemesanan.setStatus("tanggungan");
                    double jum = Double.parseDouble(jumlah_barang);
                    pengiriman.setJumlahBarang(jum);
                    pengiriman.setIdBarang(pemesanan.tampilIdBarang(kode_pemesanan));
                    Cookie[] ck = request.getCookies();
                    pengiriman.setUsername(ck[1].getValue());
                    pengiriman.setKodePemesanan(kode_pemesanan);
                    pengiriman.setHargaBarang(Double.parseDouble(harga_barang));
                    pemesanan.insertPemesanan();
                    pengiriman.insertPengiriman();
                    out.println("<html>\n"
                            + "    <head>\n"
                            + "        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
                            + "        <title></title>\n"
                            + "    </head>\n"
                            + "    <body bgcolor=\"856FD7\"><br><br>\n"
                            + "        <form action=\"KirimBarang\">\n"
                            + "            <table border=\"0\" align=\"center\" width=\"600\">\n"
                            + "                <tr>\n"
                            + "                    <td align=\"center\"><img src=\"image/logo.png\" width=\"380\" height=\"100\"></td>\n"
                            + "                </tr>\n"
                            + "            </table><br>\n"
                            + "            <font face=\"ESP_Bold\" size=\"6px\"><center>DATA TERSIMPAN</center></font><br>\n"
                            + "            <table border=\"0\" align=\"center\" width=\"250\"> \n"
                            + "                <tr>\n"
                            + "                    <td align=\"center\"><a href=\"form_utama_gudang.jsp\"><img src=\"image/kembali_button.png\" width=\"120\" height=\"35\"></a>\n"
                            + "                </tr>\n"
                            + "            </table>\n"
                            + "        </form>\n"
                            + "    </body>\n"
                            + "</html>");
                } else {
                    pemesanan.setStatus("terkirim");
                    double jum = Double.parseDouble(jumlah_barang);
                    pengiriman.setJumlahBarang(jum);
                    pengiriman.setIdBarang(pemesanan.tampilIdBarang(kode_pemesanan));
                    Cookie[] ck = request.getCookies();
                    pengiriman.setUsername(ck[1].getValue());
                    pengiriman.setKodePemesanan(kode_pemesanan);
                    pengiriman.setHargaBarang(Double.parseDouble(harga_barang));
                    pemesanan.insertPemesanan();
                    pengiriman.insertPengiriman();
                    out.println("<html>\n"
                            + "    <head>\n"
                            + "        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
                            + "        <title></title>\n"
                            + "    </head>\n"
                            + "    <body bgcolor=\"856FD7\"><br><br>\n"
                            + "        <form action=\"KirimBarang\">\n"
                            + "            <table border=\"0\" align=\"center\" width=\"600\">\n"
                            + "                <tr>\n"
                            + "                    <td align=\"center\"><img src=\"image/logo.png\" width=\"380\" height=\"100\"></td>\n"
                            + "                </tr>\n"
                            + "            </table><br>\n"
                            + "            <font face=\"ESP_Bold\" size=\"6px\"><center>DATA TERSIMPAN</center></font><br>\n"
                            + "            <table border=\"0\" align=\"center\" width=\"250\"> \n"
                            + "                <tr>\n"
                            + "                    <td align=\"center\"><a href=\"form_utama_gudang.jsp\"><img src=\"image/kembali_button.png\" width=\"120\" height=\"35\"></a>\n"
                            + "                </tr>\n"
                            + "            </table>\n"
                            + "        </form>\n"
                            + "    </body>\n"
                            + "</html>");
                }
            } else {
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("form_kirim_barang.jsp");
                requestDispatcher.forward(request, response);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
